﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportStore.Domain.Entities;

namespace SportStore.WebUI.Binder
{
    public class CartModelBinder : IModelBinder
    {
        private const string SessionKey = "Cart";
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //get cart from session
            Cart cart = (Cart)controllerContext.HttpContext.Session[SessionKey];
            //create the cart if it wasn't in session data 
            if (cart == null)
            {
                cart = new Cart();
                controllerContext.HttpContext.Session[SessionKey] = cart;
            }
            //return the cart
            return cart;

        }
    }
}